<template>
  <div class="login">
      <div class="log">
        <p>登录</p>
      </div>
      <div class="neirong">
        <div class="phone">
          <span class="img">
            <img :src="phone" alt="">
          </span>
          <div class="inp">
            <span class="num">+ 86</span>
            <input type="tel" name="" id="" v-model="tel">
          </div>
        </div>
        <div class="pwd">
          <span class="img">
            <img :src="lock" alt="">
          </span>
          <div class="inp">
            <input type="password" name="" id="" v-model="pwd">
            <span class="clsbtn" @click="del" v-if="pwd.length>0">
              <img :src="clb" alt="">
            </span>
          </div>
        </div>
      </div>
      <div class="btn">
        <img :src="canToLogin?unloginbtn:loginbtn" @click="login" alt="">
      </div>
      <div class="end">
        <span class="end1" @click="zhuce">立即注册</span>
        <span class="end2" @click="forget">忘记密码</span>
      </div>
  </div>
</template>
<script>
import { _ajaxlogin } from "../../service/main.js";
export default{
  data(){
    return{
      username:'',
      phone:require('../../assets/img/ph@2x.png'),
      lock:require('../../assets/img/locked@2x.png'),
      clb:require('../../assets/img/closebtn.png'),
      canToLogin: false,
      loginbtn: require("../../assets/img/lod.png"),
      unloginbtn: require("../../assets/img/blacklod.png"),
      tel: "",
      pwd: ""
    }
  },
  watch: {
    tel(newValue, oldValue) {
      if (this.pwd.trim().length > 0 && this.validatephone(this.tel)) {
        this.canToLogin = true;
      } else {
        this.canToLogin = false;
      }
    },
    pwd(newValue) {
      if (this.validatephone(this.tel)) {
        if (this.pwd.trim().length > 0) {
          this.canToLogin = true;
        } else {
          this.canToLogin = false;
        }
      } else {
        this.canToLogin = false;
      }
    }
  },
  activated() {
    (this.tel = ""), (this.pwd = "");
  },
  methods: {
    zhuce() {
      this.$router.push("/reg111");
    },
    forget() {
      this.$router.push("/password/forget");
    },
    del() {
      this.pwd = "";
    },
    validatephone(val) {
      let nub = val.replace(/\s*/g, "").replace("+86", "");
      if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(nub)) {
        return false;
      }
      return true;
    },
    login() {
      let _this = this;
      if (!this.canToLogin) {
        return;
      }
      let nub = _this.tel.replace(/\s*/g, "").replace("+86", "");
      _ajaxlogin({ mobile: nub, password: _this.pwd }).then(res => {
        this.$toast(res.message);
        if (res.errcode == 1) {
          localStorage.setItem("user", JSON.stringify(res.data));
          this.$router.push("/indexs");
        }
      });
    }
  }
}
</script>
<style scoped>
.login{
  width: 100%;
  height: 100%;
  padding: 100px 67px 0 67px;
}
.log{
  font-size: 24px;
  font-weight: 800;
  line-height: 27px;
  color: #000;
}
.pwd{
  margin: 15px 0 0 0;
}
.neirong{
  width: 100%;
  margin: 105px 0 0 0;
}
.img img{
  width: 11px;
}
.clsbtn img{
  float: right;
  width: 15px;
  position: relative;
  top: 10px;
  right: 5px;
}
.inp{
  width: 97%;
  border: 1px solid rgb(204, 204, 204);
  border-top: none;
  border-left: none;
  border-right: none;
  position: relative;
  left: 20px;
  top: -25px;
}
input{
  background:none;  
		outline:none;  
		border:none;
		height: 30px;
		line-height: 30px;
		font-family: "DINPro-Bold";
		font-size: 15px;
		color: #000;
    caret-color:#000;
    margin: 0 0 0 5px;
}
.num{
    font-size: 15px;
    font-weight: 800;
  }
.btn img{
  width: 270px;
  margin: 20px 0 0 -10px;
}
.end{
  font-size: 12px;
  color: rgb(204, 204, 204);
  line-height: 12px;
  font-weight: 600;
  margin: 10px 0 0 0;
}
.end2{
  float:right;
  margin: 0 -7px 0 0 
}
.cle img{
  width: 15px;
  margin-left: 20px;
  margin-top: -5px;
}
</style>